/**************3P Gates Demand Creation Analysis code**************
Author: Gabrielle Stein
Started: January 2020
Description: 3P Demand Creation Paper submitted to Gates Open ResearchEnumeration Output:
			 1. Table 1: 
				Summarize characteristics of those who completed the initial survey in the video arm
			 2. Table 2:
				Identify characteristics that are associated with an interest in PrEP in the video arm by running
			 	univariate and multivariate models carrying forward items that have a p-value of 0.20 and less
***************************************************************************/

/***************************************************************************
Import and set up the data;
***************************************************************************/
proc import datafile="...\3P_Gates_DemandCreation.csv" /*add file path here to refer to the CSV*/
out=work.gates1
dbms=csv REPLACE;
run;

/***************************************************************************
CROSSTABS IN TABLE 1 AND TEXT
***************************************************************************/
* In text;
proc freq data=work.gates1;
tables arm_video*consent;
tables arm_video*consent*feelings_about_video;
tables arm_video*consent*like_video;
tables arm_video*consent*characters_relatable;
tables arm_video*consent*recommend_video_scale;
tables arm_video*consent*want_to_learn_more;
tables arm_video*consent*consider_taking_prep*what_to_increase_interest___1;
tables arm_video*consent*consider_taking_prep*what_to_increase_interest___2; 
tables arm_video*consent*consider_taking_prep*what_to_increase_interest___3;
run;

* Table 1 N(%);
proc freq data=work.gates1;
tables arm_video*consent;
run;
proc freq data=work.gates1;
where setting = 1 and arm_video = 1;
tables consent;
tables educgroup;
tables food_security;
tables want_children; 
tables has_partner_r;
tables partageflag;
tables had_sex_w_partner_r;
tables had_sex_w_other_r;
run;
* Table 1 median(IQR);
proc means data=work.gates1 median q1 q3;
where setting = 1 and arm_video = 1;
var age;
var partner_age_r;
var relationship_length_months_r;
run;


/***************************************************************************
UNIVARIATE AND MULTIVARIATE IN TABLE 2 AND TEXT
Characteristics considered were: 
***************************************************************************/
data work.gates2;
set work.gates1;
if outcome ne . and arm_video =1;
run;
* Table 2 Univariate;
*Outcome;
proc freq data=work.gates2;
tables outcome;
run;
*Age;
proc means data=work.gates2 median q1 q3;
var age;
run;
proc means data=work.gates2 median q1 q3;
var age;
class outcome;
run;
proc logistic data=work.gates2;
model outcome (event='1') = age;
oddsratio age;
run;
* Education;
proc freq data=work.gates2;
tables educgroup;
run;
proc freq data=work.gates2;
tables outcome*educgroup;
run;
proc logistic data=work.gates2;
class educgroup (ref='2')/ param=ref;
where educgroup ne 99; *exclude unknown in the univarate comparison;
model outcome (event='1') = educgroup;
oddsratio educgroup;
run;
*Food security;
proc freq data=work.gates2;
tables food_security;
run;
proc freq data=work.gates2;
tables outcome*food_security;
run;
proc logistic data=work.gates2;
class food_security (ref='1')/ param=ref;
where food_security ne 99; *exclude unknown in the univarate comparison;
model outcome (event='1') = food_security;
oddsratio food_security;
run;
*Wanting child in next year;
proc freq data=work.gates2;
tables want_children;
run;
proc freq data=work.gates2;
tables outcome*want_children;
run;
proc logistic data=work.gates2;
class want_children (ref='0')/ param=ref;
model outcome (event='1') = want_children;
oddsratio want_children;
run;
*Primary sexual partner;
proc freq data=work.gates2;
tables has_partner_r;
run;
proc freq data=work.gates2;
tables outcome*has_partner_r;
run;
proc logistic data=work.gates2;
class has_partner_r (ref='0')/ param=ref;
model outcome (event='1') =has_partner_r;
oddsratio has_partner_r;
run;
*Age difference with partner >5 years;
proc freq data=work.gates2;
tables partageflag;
run;
proc freq data=work.gates2;
tables outcome*partageflag;
run;
proc logistic data=work.gates2;
class partageflag (ref='0')/ param=ref;
model outcome (event='1') =partageflag;
oddsratio partageflag;
run;
*Partnership duration;
proc freq data=work.gates2;
tables RelLengthFlag;
run;
proc freq data=work.gates2;
tables outcome*RelLengthFlag;
run;
proc logistic data=work.gates2;
class RelLengthFlag (ref='3')/ param=ref;
model outcome (event='1') =RelLengthFlag;
oddsratio RelLengthFlag;
run;
*Sex with primary partner in prior 30 days;
proc freq data=work.gates2;
tables had_sex_w_partner_r;
run;
proc freq data=work.gates2;
tables outcome*had_sex_w_partner_r;
run;
proc logistic data=work.gates2;
class had_sex_w_partner_r (ref='0')/ param=ref;
model outcome (event='1') =had_sex_w_partner_r;
oddsratio had_sex_w_partner_r;
run;
*Sex with anyone else;
proc freq data=work.gates2;
tables had_sex_w_other_r;
run;
proc freq data=work.gates2;
tables outcome*had_sex_w_other_r;
run;
proc logistic data=work.gates2;
class had_sex_w_other_r (ref='0')/ param=ref;
model outcome (event='1') =had_sex_w_other_r;
oddsratio had_sex_w_other_r;
run;
*Risk taking;
proc freq data=work.gates2;
tables take_risks_scale;
run;
proc freq data=work.gates2;
tables outcome*take_risks_scale;
run;
proc logistic data=work.gates2;
class take_risks_scale (ref='3')/ param=ref;
model outcome (event='1') =take_risks_scale;
where take_Risks_scale ne 99;
oddsratio take_risks_scale;
run;
*Perception of participant�s own HIV risk;
proc freq data=work.gates2;
tables riskflag;
run;
proc freq data=work.gates2;
tables outcome*riskflag;
run;
proc logistic data=work.gates2;
class riskflag (ref='0')/ param=ref;
model outcome (event='1') =riskflag;
where riskflag ne 99;
oddsratio riskflag;
run;
*Whether participant had heard of PrEP prior to the survey;
proc freq data=work.gates2;
tables heard_of_prep;
run;
proc freq data=work.gates2;
tables outcome*heard_of_prep;
run;
proc logistic data=work.gates2;
class heard_of_prep (ref='0')/ param=ref;
model outcome (event='1') =heard_of_prep;
oddsratio heard_of_prep;
run;
*How they had heard of PrEP;
	*home visit;
proc freq data=work.gates2;
tables how_heard_of_prep___1;
run;
proc freq data=work.gates2;
tables outcome*how_heard_of_prep___1;
run;
proc logistic data=work.gates2;
class how_heard_of_prep___1 (ref='0')/ param=ref;
model outcome (event='1') =how_heard_of_prep___1;
oddsratio how_heard_of_prep___1;
run;
	*Media;
proc freq data=work.gates2;
tables how_heard_of_prep___2;
run;
proc freq data=work.gates2;
tables outcome*how_heard_of_prep___2;
run;
proc logistic data=work.gates2;
class how_heard_of_prep___2 (ref='0')/ param=ref;
model outcome (event='1') =how_heard_of_prep___2;
oddsratio how_heard_of_prep___2;
run;
	*Friend or family;
proc freq data=work.gates2;
tables how_heard_of_prep___3;
run;
proc freq data=work.gates2;
tables outcome*how_heard_of_prep___3;
run;
proc logistic data=work.gates2;
class how_heard_of_prep___3 (ref='0')/ param=ref;
model outcome (event='1') =how_heard_of_prep___3;
oddsratio how_heard_of_prep___3;
run;
	*Other;
proc freq data=work.gates2;
tables how_heard_of_prep___66;
run;
proc freq data=work.gates2;
tables outcome*how_heard_of_prep___66;
run;
proc logistic data=work.gates2;
class how_heard_of_prep___66 (ref='0')/ param=ref;
model outcome (event='1') =how_heard_of_prep___66;
oddsratio how_heard_of_prep___66;
run;
	*Prefer not to answer;
proc freq data=work.gates2;
tables how_heard_of_prep___99;
run;
proc freq data=work.gates2;
tables outcome*how_heard_of_prep___99;
run;
proc logistic data=work.gates2;
class how_heard_of_prep___99 (ref='0')/ param=ref;
model outcome (event='1') =how_heard_of_prep___99;
oddsratio how_heard_of_prep___99;
run;
*Whether the participant liked the video;
proc freq data=work.gates2;
tables likevidflag;
run;
proc freq data=work.gates2;
tables outcome*likevidflag;
run;
proc logistic data=work.gates2;
class likevidflag (ref='7')/ param=ref;
model outcome (event='1') =likevidflag;
where likevidflag ne 99;
oddsratio likevidflag;
run;

* Table 2 Multivariate;
data work.gates3;
set work.gates2;
take_risks_scale1 = take_risks_scale;
if take_risks_scale = 99 then do; 
	take_risks_scale1 = 3; 
end;

* In MV model create a 'dummy' variable for 'prefer not to answer' in risk taking. Exclude in reporting;
if take_risks_scale ne 99 then do; mv_risk_flag2 = 0; end;
if take_risks_scale = 99 then do; mv_risk_flag2 = 1; end;
run;


*Full model for AORs and CIs;
ods output globaltests=getlrtestfull  OddsRatios=ORfull;
proc logistic data=work.gates3;
	class
	has_partner_r (ref='0') 
	food_security (ref='1')
	partageflag (ref='0') 
	rellengthflag2(ref='3') 
	take_risks_scale1 (ref='3')
	mv_risk_flag2 (ref='0')
	/ param=ref;
	model outcome (event='1') = has_partner_r partageflag RelLengthFlag2 take_risks_scale1 mv_risk_flag2 food_security 
	;
run;

*Get interaction p-value by Likelihood Ratio tests;

* reduced model1: LR p-value for has_partner_r;
ods output globaltests=getlrtest1;
proc logistic data=work.gates3;
	class 
	food_security (ref='1')
	partageflag (ref='0') 
	rellengthflag2(ref='3') 
	take_risks_scale1 (ref='3')
	mv_risk_flag2 (ref='0')
	/ param=ref;
	model outcome (event='1') = partageflag RelLengthFlag2 take_risks_scale1 food_security mv_risk_flag2
	;
run;
* reduced model2: LR p-value for partageflag;
ods output globaltests=getlrtest2;* ;
proc logistic data=work.gates3;
	class 
	has_partner_r (ref='0') 
	rellengthflag2(ref='3') 
	take_risks_scale1 (ref='3')
	food_security (ref='1')
	mv_risk_flag2 (ref='0')
	/ param=ref;
	model outcome (event='1') = has_partner_r RelLengthFlag2 take_risks_scale1 mv_risk_flag2 food_security
	;
run;
* reduced model3: LR p-value for RelLengthFlag2;
ods output globaltests=getlrtest3;;
proc logistic data=work.gates3;
	class 
	has_partner_r (ref='0') 
	partageflag (ref='0') 
	take_risks_scale1 (ref='3')
	food_security (ref='1')
	mv_risk_flag2 (ref='0')
	/ param=ref;
	model outcome (event='1') = has_partner_r partageflag take_risks_scale1 mv_risk_flag2 food_security
	;
run;
* reduced model4: LR p-value for take_risks_scale;
ods output globaltests=getlrtest4;
proc logistic data=work.gates3;
	class 
	has_partner_r (ref='0') 
	partageflag (ref='0') 
	rellengthflag2(ref='3')
	food_security (ref='1')
	mv_risk_flag2 (ref='0')
	/ param=ref;
	model outcome (event='1') = has_partner_r partageflag RelLengthFlag2 food_security mv_risk_flag2
	;
run;
* reduced model5: LR p-value for food_security;
ods output globaltests=getlrtest5; *;
proc logistic data=work.gates3;
	class 
	has_partner_r (ref='0') 
	partageflag (ref='0') 
	rellengthflag2(ref='3')
	take_risks_scale1(ref='3')
	mv_risk_flag2 (ref='0')
	/ param=ref;
	model outcome (event='1') = has_partner_r partageflag RelLengthFlag2 take_risks_scale1 mv_risk_flag2
	;
run;
* reduced model6: LR p-value for mv_risk_flag2;
ods output globaltests=getlrtest6;
proc logistic data=work.gates3;
	class 
	has_partner_r (ref='0') 
	partageflag (ref='0') 
	rellengthflag2(ref='3')
	take_risks_scale1(ref='3')
	food_security (ref='1')
	/ param=ref;
	model outcome (event='1') = has_partner_r partageflag RelLengthFlag2 take_risks_scale1 food_security
	;
run;

* Compute pvalues;
data modeltest1(keep=comparison LRpvalue);
merge getlrtestfull (rename=(chisq=chisq_full df=df_full))
	getlrtest1 (rename=(chisq=chisq_reduced1 df=df_reduced1))
	;
	if test='Likelihood Ratio';
	df=df_full-df_reduced1;  
	chisq=chisq_full-chisq_reduced1;  
	LRpvalue=1-probchi(chisq,df);
	comparison='1: Full model compared to reduced model 1. LR for has_partner_r'; 
run;
data modeltest2 (keep=comparison LRpvalue);
merge getlrtestfull (rename=(chisq=chisq_full df=df_full))
	getlrtest2 (rename=(chisq=chisq_reduced2 df=df_reduced2))
	;
	if test='Likelihood Ratio';
	df=df_full-df_reduced2;  
	chisq=chisq_full-chisq_reduced2;  
	LRpvalue=1-probchi(chisq,df);
	comparison='2: Full model compared to reduced model 2. LR for partageflag'; 
run;
data modeltest3 (keep=comparison LRpvalue);
merge getlrtestfull (rename=(chisq=chisq_full df=df_full))
	getlrtest3 (rename=(chisq=chisq_reduced3 df=df_reduced3))
	;
	if test='Likelihood Ratio';
	df=df_full-df_reduced3;  
	chisq=chisq_full-chisq_reduced3;  
	LRpvalue=1-probchi(chisq,df);
	comparison='3: Full model compared to reduced model 3. LR for RelLengthFlag2'; 
run;
data modeltest4 (keep=comparison LRpvalue);
merge getlrtestfull (rename=(chisq=chisq_full df=df_full))
	getlrtest4 (rename=(chisq=chisq_reduced4 df=df_reduced4))
	;
	if test='Likelihood Ratio';
	df=df_full-df_reduced4;  
	chisq=chisq_full-chisq_reduced4;  
	LRpvalue=1-probchi(chisq,df);
	comparison='4: Full model compared to reduced model 4. LR for take_risks_scale'; 
run;
data modeltest5 (keep=comparison LRpvalue);
merge getlrtestfull (rename=(chisq=chisq_full df=df_full))
	getlrtest5 (rename=(chisq=chisq_reduced5 df=df_reduced5))
	;
	if test='Likelihood Ratio';
	df=df_full-df_reduced5;  
	chisq=chisq_full-chisq_reduced5;  
	LRpvalue=1-probchi(chisq,df);
	comparison='5: Full model compared to reduced model 5. LR for food_security'; 
run;
data modeltest6 (keep=comparison LRpvalue);
merge getlrtestfull (rename=(chisq=chisq_full df=df_full))
	getlrtest6 (rename=(chisq=chisq_reduced6 df=df_reduced6))
	;
	if test='Likelihood Ratio';
	df=df_full-df_reduced6;  
	chisq=chisq_full-chisq_reduced6;  
	LRpvalue=1-probchi(chisq,df);
	comparison='6: Full model compared to reduced model 6. LR for mv_risk_flag2'; 
run;
data modeltest;
	set modeltest1 modeltest2 modeltest3 modeltest4 modeltest5 modeltest6;
run;
proc print data=modeltest;run;
